package com.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.TrainDao;
import com.entity.TrainInfo;

public class TrainDaoImpl extends BaseDao implements TrainDao {

	// 查询所有车次
	@Override
	public List<TrainInfo> getAllTrain() {
		System.out.println("输出测试");
		System.out.println("https://gitee.com/hubing2005/TrainSys.git");
		System.out.println("https://gitee.com/hubing2005/TrainSys.git");
		String sql = "SELECT * from train_info;";
		ResultSet rs = this.executeQuery(sql, null);
		List<TrainInfo> trainList = null;
		try {
			trainList=new ArrayList<TrainInfo>();
			while (rs.next()) {
				TrainInfo train = new TrainInfo();
				train.setTrain_no(rs.getString(1));
				train.setStart_station(rs.getString(2));
				train.setArrival_station(rs.getString(3));
				train.setStart_time(rs.getString(4));
				train.setArrival_time(rs.getString(5));
				train.setType(rs.getString(6));
				train.setRuntime(rs.getString(7));
				train.setMile(rs.getDouble(8));
				trainList.add(train);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
		return trainList;
	}

	// 查询指定车次
	/**
	 * 查询指定车次
	 * 
	 * @param no
	 *            车次编号
	 * @return 车次实体
	 */
	@Override
	public TrainInfo findTrain(String no) {
		String sql = "SELECT * from train_info where train_no=?;";
		ResultSet rs = this.executeQuery(sql, new Object[] { no });
		TrainInfo train = new TrainInfo();
		try {
			while (rs.next()) {
				train.setTrain_no(rs.getString(1));
				train.setStart_station(rs.getString(2));
				train.setArrival_station(rs.getString(3));
				train.setStart_time(rs.getString(4));
				train.setArrival_time(rs.getString(5));
				train.setType(rs.getString(6));
				train.setRuntime(rs.getString(7));
				train.setMile(rs.getDouble(8));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeAll();
		}
		return train;
	}

	// 删除车次
	@Override
	public int delTrain(String no) {
		String sql = "DELETE FROM train_info where train_no=?;";
		int temp = this.executeUpdate(sql, new Object[] { no });
		return temp;
	}

}
